package com.lo23.data.objgroup;

import com.lo23.data.utils.DataUtils;
import com.lo23.data.xml.ObjectFactory;
import com.lo23.data.xml.XMLData;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.xml.bind.JAXBElement;

public class Group {

	private XMLData.XMLGroup _group;

	/**
	 * @param group
	 */
	public Group(XMLData.XMLGroup group) {
		_group = group;
	}

	/**
	 * Return the group
	 * 
	 * @return the xml data
	 */
	public XMLData.XMLGroup getXML() {
		return _group;
	}

	/**
	 * Get the group name
	 * 
	 * @return the name
	 */
	public String getName() {
		return DataUtils.nullString(_group.getName());
	}

	/**
	 * Get the group ID
	 * 
	 * @return the gID
	 */
	public String getgID() {
		return DataUtils.nullString(_group.getGID());
	}

	/**
	 * Add friend to the group You must give the friend object as parameter
	 * 
	 * @return void
	 */
	public boolean addFriend(Friend friend) {
		if (!hasFriend(friend.getuID())) {
			JAXBElement<Object> xmlFriendReference = (new ObjectFactory())
					.createXMLDataXMLGroupFriendItems(friend.getXML());

			_group.getFriendItems().add(xmlFriendReference);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get list of all friends in the group
	 * 
	 * @return the list of people in the group
	 */
	public List<Friend> getFriends() {
		List<JAXBElement<Object>> objectList = _group.getFriendItems();
		List<Friend> returnList = new ArrayList<Friend>();

		for (JAXBElement<Object> anItem : objectList) {
			XMLData.XMLUser aXMLItem = (XMLData.XMLUser) anItem.getValue();
			returnList.add(new Friend(aXMLItem));
		}
		return returnList;
	}

	/**
	 * Set the group's name
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		_group.setName(name);
	}

	/**
	 * Set a picture for the group
	 * 
	 * @param The
	 *            path of picture to set
	 * 
	 */
	public void setPicture(String path) {
		(new File("images")).mkdir();
		String copyPath = "images/" + getgID() + ".jpg";
		DataUtils.copyImageToPath(path, copyPath, "jpg");
		_group.setPath(copyPath);
	}

	/**
	 * Get picture of the group
	 * 
	 * @return the picture
	 */
	public ImageIcon getPicture() {
		if (_group.getPath() == null) {
			return null;
		} else {
			return new ImageIcon(_group.getPath());
		}
	}

//	/**
//	 * Set the group's ID
//	 * 
//	 * @param gID
//	 *            the gID to set
//	 */
//	public void setgID(String gID) {
//		_group.setGID(gID);
//	}

	/**
	 * Check if group has a certain user
	 * 
	 * @param uID
	 *            the friend uID
	 * 
	 * @return return true if user is present, false otherwise
	 */
	public boolean hasFriend(String uID) {
		List<Friend> friends = getFriends();
		for (Friend anItem : friends) {
			if (anItem.getuID().equals(uID)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Delete a friend from group
	 * 
	 * @param the
	 *            id of the friend to delete
	 * 
	 * @return Boolean if friend has been deleted
	 */
	public Boolean deleteFriend(String fid) {
		List<Friend> friendList = this.getFriends();
		int i = 0;
		for (Friend com : friendList) {
			if (com.getuID().equals(fid)) {
				_group.getFriendItems().remove(i);
				return true;
			}
			i++;
		}
		return false;
	}

}
